package site.user.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modules.recruit.entity.ApplyManager;
import modules.recruit.entity.Experience;
import modules.recruit.service.ExperienceService;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.guinea.core.service.EntityService;
import com.guinea.core.utils.StringUtils;
import com.guinea.core.web.StrutsAjaxAction;

import ex.recruit.service.ApplyManagerServiceEx;

public class ExperienceActionSite extends StrutsAjaxAction {
	private ExperienceService experienceService;

	private ApplyManagerServiceEx applyManagerServiceEx;

	private ApplyManager applyManager;

	private Long applyManagerId;

	public EntityService getEntityService() {
		return experienceService;
	}

	public String entity2json(Object object) {
		Experience experience = (Experience) object;
		String result = "";
		result += "{";
		result += "\"id\" : \""
				+ (experience.getId() == null ? "" : experience.getId()
						.longValue()
						+ "") + "\"";
		result += ", \"managerId\" : \""
				+ (experience.getManager() == null ? "" : experience
						.getManager().getId().longValue()
						+ "") + "\"";
		result += ", \"period\" : \""
			+ StringUtils.defaultString(experience.getPeriod()) + "\"";
		result += ", \"period2\" : \""
			+ StringUtils.defaultString(experience.getPeriod2()) + "\"";
		result += ", \"corp\" : \""
				+ StringUtils.defaultString(experience.getCorp()) + "\"";
		result += ", \"job\" : \""
				+ StringUtils.defaultString(experience.getJob()) + "\"";
		result += "}";
		return result;
	}

	protected String saveWrapper(String text) {
		return "<script>window.parent.successExperience('" + text
				+ "')</script>";
	}

	public ExperienceService getExperienceService() {
		return experienceService;
	}

	public void setExperienceService(ExperienceService experienceService) {
		this.experienceService = experienceService;
	}

	public ApplyManagerServiceEx getApplyManagerServiceEx() {
		return applyManagerServiceEx;
	}

	public void setApplyManagerServiceEx(ApplyManagerServiceEx applyManagerServiceEx) {
		this.applyManagerServiceEx = applyManagerServiceEx;
	}

	/**
	 * 初始化业务对象的函数.
	 */
	protected void findEntity(ActionForm form, HttpServletRequest request) {
		super.findEntity(form, request);
		applyManagerId = (Long) getEntityId(request, "applyManagerId",
				Long.class);
		if (applyManagerId != null) {
			applyManager = getApplyManagerServiceEx().getApplyManager(
					applyManagerId);
		}
	}

	protected void initProperties() {
		super.initProperties();
		applyManagerId = null;
		applyManager = null;
	}

	protected boolean validateApplyManager(HttpServletRequest request) {
		if (applyManager == null) {
			return valid = false;
		}
		return true;
	}

	public ActionForward ajaxListValidator(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		initFirst(form, request);
		if (!validateApplyManager(request))
			return handleApplyManagerMissing(mapping, request, response);
		return null;
	}

	/**
	 * 取得SkoruShareholder对象列表
	 */
	public ActionForward ajaxList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		List list = getApplyManagerServiceEx().getExperiencesOfManager(applyManagerId);
		String result = collection2json(list);
		renderText(response, "{\"data\":" + result + "}");
		return null;
	}

	public ActionForward ajaxSaveValidator(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		initFirst(form, request);
		if (!validateApplyManager(request))
			return handleApplyManagerMissing(mapping, request, response);
		if (!validateForm(mapping, form, request))
			return handleFormErrors(mapping, form, request, response);
		return null;
	}

	protected void doInitEntity(HttpServletRequest request, ActionForm form) {
		super.doInitEntity(request, form);
		if (isCreateOperation) {
			Experience experience = (Experience) entity;
			experience.setManager(applyManager);
			entity = experience;
		}
	}

	protected ActionForward handleApplyManagerMissing(ActionMapping mapping,
			HttpServletRequest request, HttpServletResponse response) {
		renderText(response, "{\"error\":\"找不到股权结构记录!\"}");
		return null;
	}

}
